*CHECK WHETHER THOSE WHO MOVED TO OW WERE MORE LIKELY TO ALSO WORK IN OW THAN BEFORE

/* WHAT IS THE SHARE OF IN-MOVERS WHO:
- ARE RETIREES?
- ARE IN LF / WORKING?
- ARE COMMUTING OUT OF CANTON / WORK IN THE CANTON?
*/

cap frame drop commuters_residents
frame copy default commuters_residents
frame change commuters_residents

cd "$mypathRR/Results/Resident/appendix_commuters"

// gen treatment variable
cap drop treat_i
gen treat_i=.
replace treat_i=0 if ctrl_ir_60_80==1
replace treat_i=1 if taxinc_thre2==1
label var treat_i "Income tax treatment"







sort persid year year
drop if year>2010

gen km_dt = .
	replace km_dt = z205s1 / 220 / 2 / 0.7 if z205s1 < = 10500
	replace km_dt = (z205s1 - 10500) / 0.4 / 220 / 2 + 15000 / 220 / 2 if z205s1 > 10500 & z205s1 < .
	replace km_dt = 0 if z205s1 == .
	replace km_dt = 0 if z205s1 == 0

gen km_ep = .
	replace km_ep = z206s1 / 220 / 2 / 0.7 if z206s1 < = 10500
	replace km_ep = (z206s1 - 10500) / 0.4 / 220 / 2 + 15000 / 220 / 2 if z206s1 > 10500 & z206s1 < .
	replace km_ep = 0 if z206s1 == .
	replace km_ep = 0 if z206s1 == 0

gen train_dt = (z201s1 > 0 & z201s1 <.)

	
forval n = 1/7 {
sum km_dt if ste_prigde_code == `n', de // in each municipality, about 5% commute > 25km
}
forval n = 1/7 {
sum km_ep if ste_prigde_code == `n', de // in each municipality, less than 1% of spouses commute > 25km ! 
}


// Commuting patterns main taxpayer vs. spouse by municipality
forval n = 1/7 {
quiet{
sum km_dt if km_dt == 0 & ste_prigde_code == `n',
local a = r(N)
sum km_dt if  ste_prigde_code == `n',
local b = r(N)
local share_dt = (1 - `a'/`b')*100
local share_dt = trim("`: display %5.1f `share_dt''")

sum km_ep if km_ep == 0  & ste_prigde_code == `n',
local a = r(N)
sum km_ep if  ste_prigde_code == `n',
local b = r(N)
local share_ep = (1 - `a'/`b')*100
local share_ep = trim("`: display %5.1f `share_ep''")
}

	tw 	(hist km_dt if  km_dt > 1 & km_dt < 151 & ste_prigde_code == `n', 		///
		percent width(2.5) col(blue%20))										///
		(hist km_ep if  km_ep > 1 & km_ep < 151 & ste_prigde_code == `n',		///
		percent width(2.5)	col(red%30))										///
		, xline(25, lcolor(red)) xlab(0(25)150) ylab(0(5)30) 					///
		text(25 100 "Share commuting" "Main taxpayer: `share_dt'%"				///
		"Spouse: `share_ep'%", justification(left) linegap(*1.5) 				///
		box fcol(white) margin(small)) 											///
		legend(order(1 "Main taxpayers" 2 "Spouses")) name(km_`n', replace)
}



// Commuting patterns pre- vs. post-reform by municipality
forval n = 1/7 {
quiet{
sum km_dt if km_dt == 0 & ste_prigde_code == `n' & year <2006
local a = r(N)
sum km_dt if  ste_prigde_code == `n'  & year < 2006
local b = r(N)
local share_pre = (1 - `a'/`b')*100
local share_pre = trim("`: display %5.1f `share_pre''")

sum km_dt if km_dt == 0 & ste_prigde_code == `n' & year >2005
local a = r(N)
sum km_dt if  ste_prigde_code == `n'  & year >2005
local b = r(N)
local share_post = (1 - `a'/`b')*100
local share_post = trim("`: display %5.1f `share_post''")

}

	tw 	(hist km_dt if  km_dt > 1 & km_dt < 151 & ste_prigde_code == `n' & year <2006, 		///
		percent width(2.5) col(blue%20))													///
		(hist km_dt if  km_dt > 1 & km_dt < 151 & ste_prigde_code == `n' & year >2005,		///
		percent width(2.5)	col(red%30))													///
		, xline(25, lcolor(red)) xlab(0(25)150) ylab(0(5)30) 								///
		text(25 100 "Share commuting" "Pre 2006: `share_pre'%"								///
		"Post 2006: `share_post'%", justification(left) linegap(*1.5) 						///
		box fcol(white) margin(small)) 														///
		legend(order(1 "Pre 2006" 2 "Post 2006")) name(kmprepost_`n', replace)
}



// Commuting patterns pre- vs. post-reform in treatment group
quiet{
sum km_dt if km_dt == 0 & 	year <2006 & treat_i == 1 & retiree01_1dt == 0
local a = r(N)
sum km_dt if 				year <2006 & treat_i == 1 & retiree01_1dt == 0
local b = r(N)
local share_pre = (1 - `a'/`b')*100
local share_pre = trim("`: display %5.1f `share_pre''")

sum km_dt if km_dt == 0 & 	year >2005 & treat_i == 1 & retiree01_1dt == 0
local a = r(N)
sum km_dt if 				year >2005 & treat_i == 1 & retiree01_1dt == 0
local b = r(N)
local share_post = (1 - `a'/`b')*100
local share_post = trim("`: display %5.1f `share_post''")


sum train_dt if train_dt == 0 & year <2006 & treat_i == 1 & retiree01_1dt == 0
local a = r(N)
sum train_dt if 				year <2006 & treat_i == 1 & retiree01_1dt == 0
local b = r(N)
local shoev_pre = (1 - `a'/`b')*100
local shoev_pre = trim("`: display %5.1f `shoev_pre''")

sum train_dt if train_dt == 0 & year >2005 & treat_i == 1 & retiree01_1 == 0
local a = r(N)
sum train_dt if 				year >2005 & treat_i == 1 & retiree01_1 == 0
local b = r(N)
local shoev_post = (1 - `a'/`b')*100
local shoev_post = trim("`: display %5.1f `shoev_post''")
}
	tw 	(hist km_dt if  km_dt > 0 & km_dt < 151 & year <2006 & treat_i == 1 & retiree01_1dt == 0, 	///
		percent width(5) start(0) col(blue%20))											///
		(hist km_dt if  km_dt > 0 & km_dt < 151 & year >2005 & treat_i == 1 & retiree01_1dt == 0,	///
		percent width(5) start(0) col(red%30))										///
		, xline(25, lcolor(red)) xlab(0(25)150) ylab(0(5)30) 					///
		text(25 110 "Share commuting by car:" "Pre-2006: `share_pre'%"			///
		"Post-2006: `share_post'%" " " "Share commuting by public transport:" 	///
		"Pre-2006: `shoev_pre'%" "Post-2006: `shoev_post'%"						///
		, justification(left) linegap(*1.5) box fcol(white) margin(small))		///
		xtitle("Estimated driving distance to workplace")						/// 
		ytitle("Percent of taxpayers" "(in treatment group and who commute by car)")						///
		legend(order(1 "Pre-2006" 2 "Post-2006")) name(kmtreat_prepost, replace)
graph export "hist_pre-post_treated.pdf", as(pdf) replace




// Commuting patterns pre- vs. post-reform overall
quiet{
sum km_dt if km_dt == 0 & 	year <2006 & retiree01_1dt == 0
local a = r(N)
sum km_dt if 				year <2006 & retiree01_1dt == 0
local b = r(N)
local share_pre = (1 - `a'/`b')*100
local share_pre = trim("`: display %5.1f `share_pre''")

sum km_dt if km_dt == 0 & 	year >2005 & retiree01_1dt == 0
local a = r(N)
sum km_dt if 				year >2005 & retiree01_1dt == 0
local b = r(N)
local share_post = (1 - `a'/`b')*100
local share_post = trim("`: display %5.1f `share_post''")


sum train_dt if train_dt == 0 & year <2006 & retiree01_1dt == 0
local a = r(N)
sum train_dt if 				year <2006 & retiree01_1dt == 0
local b = r(N)
local shoev_pre = (1 - `a'/`b')*100
local shoev_pre = trim("`: display %5.1f `shoev_pre''")

sum train_dt if train_dt == 0 & year >2005 & retiree01_1dt == 0
local a = r(N)
sum train_dt if 				year >2005 & retiree01_1dt == 0
local b = r(N)
local shoev_post = (1 - `a'/`b')*100
local shoev_post = trim("`: display %5.1f `shoev_post''")
}
	tw 	(hist km_dt if  km_dt > 0 & km_dt < 151 & year <2006 & retiree01_1dt == 0, 	///
		percent width(5) start(0) col(blue%20))											///
		(hist km_dt if  km_dt > 0 & km_dt < 151 & year >2006 & retiree01_1dt == 0,	///
		percent width(5) start(0) col(red%30))										///
		, xline(25, lcolor(red)) xlab(0(25)150) ylab(0(5)30) 					///
		text(25 110 "Share commuting by car:" "Pre-2006: `share_pre'%"			///
		"Post-2006: `share_post'%" " " "Share commuting by public transport:" 	///
		"Pre-2006: `shoev_pre'%" "Post-2006: `shoev_post'%"						///
		, justification(left) linegap(*1.5) box fcol(white) margin(small))		///
		xtitle("Estimated driving distance to workplace")						/// 
		ytitle("Percent of taxpayers" "(who commute by car)")						///
		legend(order(1 "Pre-2006" 2 "Post-2006")) name(km_prepost, replace)
graph export "hist_pre-post.pdf", as(pdf) replace






* 2 TREATMENTS 

collapse (count) Inmovers=persid (sum) retiree retiree01_1 retiree01_3 retiree01_2sp ///
employee self_emp woaufenthalt inlf01_1 commuter01 oev01 auto01 ///
(mean) avgtt=avgt_stek_taxable (mean) avgtg=avgt_stek_gross (mean) mtr=mtr_total , by(year treat_i)
label var Inmovers "Residents"
label var avgtt "Average tax rate on taxable income"
label var avgtg "Average tax rate on gross income"
label var mtr "Marginal tax rate on taxable income"
label var treat_i "Treatment"
label var year "Year"

xtset treat_i year
tsfill, full

foreach var in Inmovers retiree retiree01_1 retiree01_3 retiree01_2sp ///
employee self_emp woaufenthalt inlf01_1 commuter01 oev01 auto01 {
replace `var'=0 if `var'==.
}

gen Period1=0
replace Period1=1 if year>=2006&year<2008
gen DiD1=treat_i*Period1

gen Period2=0
replace Period2=1 if year>=2008
gen DiD2=treat_i*Period2

gen weight=Inmovers


* GEN SHARE OF RETIREES / WORKERS / COMMUTERS
foreach var in retiree retiree01_1 retiree01_3 retiree01_2sp ///
employee self_emp woaufenthalt inlf01_1 commuter01 oev01 auto01 {
gen sh_`var' = `var' / Inmovers
}

list year sh_* if treat_i == 1
list year sh_* if treat_i == 0
list year sh_* if treat_i == .

label var retiree01_1 "Both partners are retirees: no labor income in couple"
label var retiree01_2 "At least one partner is retiree: some pension income in couple"
label var retiree01_2sp "Both partners are retirees: both have pension income each"
label var retiree01_3 "Retiree household with pension income and no labor income"
label var inlf01_1 "Household has some labor income in couple"


label var sh_retiree01_1	"Share non-working (no labor income)"
label var sh_retiree01_3	"Share of retirees (pensions, no labor income)"
label var sh_commuter01		"Share of out-of-canton commuters"
label var sh_woaufenthalt	"Share with weekly residence outside OW"
label var sh_inlf01_1	 	"Share active in labor force"
label var sh_employee		"Share of employees"
label var sh_self_emp		"Share of self-employed"



* PLOTS

replace year = year +0.15

foreach var in sh_retiree01_3 sh_retiree01_1  sh_commuter01 sh_woaufenthalt 		///
				sh_inlf01_1 sh_employee sh_self_emp	{
local mylab
if `var' == sh_retiree01_1 | `var' == sh_retiree01_3 | `var' == sh_commuter01 | `var' == sh_self_emp {
	local mylab 0(.05)0.3
	di "`mylab'"
}
else if `var' == sh_woaufenthalt {
	local mylab 0(.005)0.03
}
else if `var' == sh_employee {
	local mylab 0.4(.05)0.7
}
else if `var' == sh_inlf01_1 {
	local mylab 0(.1)1
}
else {
	local mylab
}


gr tw (connect `var' year if treat_i == 0) 										///
      (connect `var' year if treat_i == 1) 										///
      (line `var' year if treat_i == ., lpat(dash) lw(*2)) , 					///
	  legend(order(1 "control" 2 "treated" 3 "rest") rows(1))					///
	  xtitle("") xlab(2002(2)2010) xline(2002, lcolor(gray) lpat(dash))			///
	  xline(2006, lcolor(red)) xline(2008, lcolor(green))						///
	  ylab(`mylab') xsize(12) ysize(12) name(`var'_resi, replace)
graph export "share_`var'-tr_ctr_rest.pdf", as(pdf) replace
}

frame change default
